///////////////////////////////////////////////////////////////////////////////
////  (c) Chaichuk AU (chaichuk@mail.ru), 								   ////
////  2005, Chelyabinsk, NTC Privodnaya Tehnika (momentum@chel.surnet.ru)  ////
///////////////////////////////////////////////////////////////////////////////

#ifndef __ANYBUS_H_
#define __ANYBUS_H_

#include <inttypes.h>

#define ABS_AP_MIN 		(1<<7)
#define ABS_AP_MOUT		(1<<6) 
#define ABS_AP_EVNT		(1<<5) 
#define ABS_ACTION 		(1<<4) 
#define ABS_LOCK		(1<<3) 
#define ABS_AP_IN 		(1<<2) 
#define ABS_AP_OUT 		(1<<1)  
#define ABS_AP_FBCTRL 	(1<<0) 

#define ABS_AB_MIN 		(1<<7)
#define ABS_AB_MOUT		(1<<6) 
#define ABS_AB_EVNT		(1<<5) 
#define ABS_INIT 		(1<<4) 
#define ABS_UPDATED		(1<<3) 
#define ABS_AB_IN 		(1<<2) 
#define ABS_AB_OUT 		(1<<1)  
#define ABS_AB_FBCTRL 	(1<<0) 


//PLX interrupt register
#define PLX_INTR_REG	0x4C

//PLX interrupt flags
#define LINT1_ENABLE	(1<<0)
#define LINT1_HIGH		(1<<1)
#define LINT1_ACTIVE	(1<<2)

#define LINT2_ENABLE	(1<<3)
#define LINT2_HIGH		(1<<4)
#define LINT2_ACTIVE	(1<<5)

#define PCI_INTR_ENABLE (1<<6)
#define SOFT_INTR		(1<<7)

#define LINT1_EDGE		(1<<8)
#define LINT2_EDGE		(1<<9)

#define LINT1_ACK		(1<<10)
#define LINT2_ACK		(1<<11)


typedef struct abs_module_s {
	
	uint8_t 	*input_data_area_ptr;
	uint8_t 	*output_data_area_ptr;
	uint8_t		*mailbox_input_area_ptr;
	uint8_t 	*mailbox_output_area_ptr;
	uint8_t 	*fieldbus_spec_area_ptr;
	uint8_t 	*control_register_area_ptr;
	uint8_t		*indic_register_rw_ptr;
	uint8_t		*indic_register_ro_ptr;
	
	int			abs_initialised;
	int			in_area_free;
	int			out_area_free;
	int			fbctrl_area_free;
	int			mbx_out_new;
	int			mbx_in_free;
	int			new_event;

	uint16_t 	total_input_len;
	uint16_t 	total_output_len; 
	uint16_t 	init_input_len;
	uint16_t 	init_output_len;
	
	uint8_t		*input_buf;
	uint8_t		*output_buf;
		
} abs_module_t;


void abs_update_status (abs_module_t *abs);
void abs_write_ind_bits (abs_module_t* abs, uint8_t c_bits, uint8_t s_bits, uint8_t t_bits);
int abs_send_mbx_msg (abs_module_t *abs, uint16_t *msg_id, const uint8_t *msg_hdr, 
						const uint8_t *msg_ext_hdr, const uint8_t *msg_data);
int abs_recv_mbx_msg (abs_module_t *abs, const uint16_t msg_id, uint8_t *msg_hdr, 
						uint8_t *msg_ext_hdr, uint8_t *msg_data, int data_size);
void abs_request_release_area (abs_module_t *abs, uint8_t request, uint8_t locked, uint8_t areas);
void abs_request_input_data_area (abs_module_t *abs);
void abs_release_input_data_area (abs_module_t *abs);
void abs_request_output_data_area (abs_module_t *abs);
void abs_release_output_data_area (abs_module_t *abs);
void abs_request_fbctrl_area (abs_module_t *abs);
void abs_release_fbctrl_area (abs_module_t *abs);
int  abs_init (abs_module_t *abs);
int  abs_sw_reset (abs_module_t *abs);
void abs_print_msg (uint8_t *msg, int size);


#endif // __ANYBUS_H_
